package com.macro.mall.controller;

import com.macro.mall.annotationUtil.CurrentUserName;
import com.macro.mall.common.api.CommonResult;
import com.macro.mall.config.OssConfig;
import com.macro.mall.model.SmsPopupAdvertise;
import com.macro.mall.service.SmsPopupAdvertiseService;
import io.swagger.annotations.Api;
import org.checkerframework.checker.units.qual.C;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;


/**
 * 后台管理弹窗广告
 * Created by macro on 2018/4/26.
 */
@Controller
@Api(tags = "SmsPopupAdvertiseController", description = "后台弹窗广告管理")
@RequestMapping("/popupAdvertise")
public class SmsPopupAdvertiseController {

    @Autowired
    private SmsPopupAdvertiseService advertiseService;
    @Autowired
    private OssConfig ossConfig;

    /**
     * 新增或修改弹窗广告
     * @author 高强
     * @Date 2020年8月28日
     * @param advertise 弹窗广告的bean
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/creatOrUpdate", method = RequestMethod.POST)
    public CommonResult<SmsPopupAdvertise> creatOrUpdate(@CurrentUserName String username,
                                                         @RequestBody SmsPopupAdvertise advertise) {
        SmsPopupAdvertise smsPopupAdvertise = advertiseService.creatOrUpdate(advertise, username);
        if (smsPopupAdvertise.getId() == null) {
            //商品未审核
            return CommonResult.failed("所选商品未通过审核");
        }
        return CommonResult.success(smsPopupAdvertise);
    }

    /**
     * 根据店铺获取弹窗广告信息
     */
    @ResponseBody
    @RequestMapping(value = "/get",method = RequestMethod.GET)
    public CommonResult get(@CurrentUserName String currentUsername){
        return advertiseService.get(currentUsername);
    }

    /**
     * 修改弹窗广告状态：1开启 0关闭
     * @param id 弹窗广告id
     * @param status 状态
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/updateStatus/{id}", method = RequestMethod.POST)
    public CommonResult updateStatus(@PathVariable(value = "id") Long id, Integer status){
        int count = advertiseService.updateStatus(id, status);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }
}
